package www.bplead.com.mydemo.activity;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import www.bplead.com.mydemo.R;
import www.bplead.com.mydemo.util.ImageFactory;
import www.bplead.com.printer.service.PrinterService;

/**
 * Created by wangyangzi on 2016/9/27.
 */
public class BluetoothPrinterActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "BluetoothPrinterActivity";

    Button qrcodeBtn;
    Button linkDeviceBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.qrcode_print);

        qrcodeBtn = (Button) this.findViewById(R.id.btn_qrcode_print);
        linkDeviceBtn = (Button) this.findViewById(R.id.btn_link_printer);

        qrcodeBtn.setOnClickListener(this);
        linkDeviceBtn.setOnClickListener(this);
    }

    Bitmap bitmap = null;
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_qrcode_print:
                if(bitmap == null){
                    bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.test_qrcode);
                    //压缩图片
                    ImageFactory imageFactory = new ImageFactory();
                    Log.v("图片压缩","压缩前大小>>" + bitmap.getByteCount());
                    bitmap = imageFactory.ratio(bitmap,50,50);
                    Log.v("图片压缩","压缩后大小>>" + bitmap.getByteCount());
                }
                ps.printImage(BluetoothPrinterActivity.this,printHandler,bitmap);
                break;
            case R.id.btn_link_printer:
                Intent intent = new Intent(this,PrinterDeviceActivity.class);
                startActivity(intent);
                break;
        }
    }

    private PrinterService ps = new PrinterService();
    private ProgressDialog bondWindow;
    private Handler printHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (bondWindow == null) {
                bondWindow = new ProgressDialog(BluetoothPrinterActivity.this);
            }
            switch (msg.what){
                case 0://正在连接
                    Log.d("打印状态","正在连接");
                    bondWindow.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                    bondWindow.setCancelable(true);
                    bondWindow.setCanceledOnTouchOutside(false);
                    bondWindow.setTitle("设备连接");
                    bondWindow.setMessage("正在连接蓝牙打印机，请稍后...");
                    bondWindow.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    bondWindow.dismiss();
                                }
                            });
                    bondWindow.show();
                    break;
                case 1://已连接
                    Log.d("打印状态","已连接");
                    String result = ps.printImage(BluetoothPrinterActivity.this,printHandler,bitmap);
                    if("ok".equals(result)){
                        Toast.makeText(BluetoothPrinterActivity.this,"打印成功！",Toast.LENGTH_SHORT).show();
                    }
                    bondWindow.dismiss();
                    break;
                case 2://无法连接
                    Log.d("打印状态","无法连接");
                    if(bondWindow != null) {
                        Toast.makeText(BluetoothPrinterActivity.this,"无法连接打印设备",Toast.LENGTH_SHORT).show();
                        bondWindow.dismiss();
                    }
                    break;
            }
        }
    };
}
